package fu.k4c.vTeam.Login;

import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import fu.k4c.vTeam.R;
import fu.k4c.vTeam.Object.Account;
import fu.k4c.vTeam.Object.Email;

public class LoginFogotPassword extends Activity {
	ProgressDialog proDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		overridePendingTransition(android.R.anim.fade_in,
				android.R.anim.fade_out);
		setContentView(R.layout.login_fogot_pass_activity);
		Button btn_back = (Button) findViewById(R.id.btn_login_fogot_pass_back);
		btn_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		Button btn_submit = (Button) findViewById(R.id.btn_login_fogot_pass_submit);
		final EditText et_email = (EditText) findViewById(R.id.et_login_fogot_pass_email);
		btn_submit.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
					if (et_email.getText().toString().trim().equals("") == false
							&& et_email.getText().toString() != null) {
						new AsyncTask<Void, Void, Void>() {
							boolean tmp1 = false;
							boolean tmp2 = false;
							boolean tmp3 = false;
							Email user;
							
							@Override
							protected Void doInBackground(Void... arg0) {
								// TODO Auto-generated method stub
								try {
									SchemeRegistry registry = new SchemeRegistry();
									registry.register(new Scheme("http",
											PlainSocketFactory.getSocketFactory(), 80));
									HttpParams httpParams = new BasicHttpParams();
									ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
											httpParams, registry);
									HttpClient client = new DefaultHttpClient(connMgr,
											httpParams);
									HttpPost post = new HttpPost(
											getString(R.string.connectionString)
													+ "vLocationService/forgotpassword");
									post.setHeader("Accept", "application/json");
									post.setHeader("Content-type", "application/json");
									HashMap<String, Object> postParameters = new HashMap<String, Object>();
									user = new Email(et_email.getText()
											.toString().trim());
									postParameters.put("UserName", user.getUsername());
									Gson gson = new Gson();
									String jsonParameters = gson.toJson(postParameters);
									post.setEntity(new StringEntity(jsonParameters));
									HttpResponse response = client.execute(post);
									HttpEntity entity = response.getEntity();
									String jsonresult = EntityUtils.toString(entity);
									Account acc = gson.fromJson(jsonresult,
											Account.class);
									if (acc.getStatus().equals("False")) {
										tmp1 = true;
									} else {
										tmp2 = true;
									}
								} catch (Exception e) {
									// TODO: handle exception
									tmp3 = true;
								}
								return null;
							}
							@Override
							protected void onPreExecute() {
								// TODO Auto-generated method stub
								proDialog = new ProgressDialog(
										LoginFogotPassword.this);
								proDialog.setMessage("Đang kết nối đến máy chủ...");
								proDialog.setCancelable(false);
								proDialog.show();
							}
							protected void onPostExecute(Void result) {
								proDialog.dismiss();
								if(tmp3)
								{
									Toast.makeText(
											getApplicationContext(),
											"Bạn vui lòng kiểm tra lại kết nối Internet",
											Toast.LENGTH_SHORT).show();
								}
								else if(tmp2)
								{
									Toast.makeText(
											getApplicationContext(),
											"Mật khẩu mới đã được gửi vào email của bạn",
											Toast.LENGTH_LONG).show();
									Intent i = new Intent(LoginFogotPassword.this,
											ChangePasswordActivity.class);
									i.putExtra("email", user.getUsername());
									startActivity(i);
								}
								else if(tmp1)
								{

									Toast.makeText(getApplicationContext(),
											"Email không tồn tại",
											Toast.LENGTH_LONG).show();
								}
							};
							
						}.execute();
						
					
				} else {
					Toast.makeText(getApplicationContext(),
							"Dữ liệu nhập vào sai",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

}
